/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.system.entity.Article;
import com.pig4cloud.pigx.system.service.ArticleService;
import com.pig4cloud.pigx.system.vo.ArticleListMargeVO;
import com.pig4cloud.pigx.system.vo.ArticleListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章列表
 *
 * @author pigx code generator
 * @date 2022-08-18 15:55:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/article" )
@Api(value = "article", tags = "文章列表管理")
public class ArticleController {

    private final  ArticleService articleService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param article 文章列表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('system_article_view')" )
    public R getArticlePage(Page page, Article article) {
        return R.ok(articleService.page(page, Wrappers.query(article)));
    }

	/**
	 * 获取轮播图
	 * @return r
	 */
	@GetMapping("/banner")
	@Inner(value = false)
	public R<List<ArticleListVO>> getBannerList() {
		return R.ok(articleService.getArticleList("Banner"));
	}


	/**
	 * 获取通知
	 * @return r
	 */
	@GetMapping("/notice")
	@Inner(value = false)
	public R<List<ArticleListVO>> getNoticeList() {
		return R.ok(articleService.getArticleList("公告"));
	}

	/**
	 * 获取通知
	 * @return r
	 */
	@GetMapping("/college")
	@Inner(value = false)
	public R<ArticleListMargeVO> getCollegeList() {
		ArticleListMargeVO vo = new ArticleListMargeVO();
		vo.setQuestions(articleService.getArticleList("快问快答"));
		vo.setBlockchain(articleService.getArticleList("区块链小知识"));
		return R.ok(vo);
	}


    /**
     * 通过id查询文章列表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('system_article_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(articleService.getById(id));
    }

    /**
     * 新增文章列表
     * @param article 文章列表
     * @return R
     */
    @ApiOperation(value = "新增文章列表", notes = "新增文章列表")
    @SysLog("新增文章列表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('system_article_add')" )
    public R save(@RequestBody Article article) {
        return R.ok(articleService.save(article));
    }

    /**
     * 修改文章列表
     * @param article 文章列表
     * @return R
     */
    @ApiOperation(value = "修改文章列表", notes = "修改文章列表")
    @SysLog("修改文章列表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('system_article_edit')" )
    public R updateById(@RequestBody Article article) {
        return R.ok(articleService.updateById(article));
    }

    /**
     * 通过id删除文章列表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除文章列表", notes = "通过id删除文章列表")
    @SysLog("通过id删除文章列表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('system_article_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(articleService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param article 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('system_article_export')" )
    public List<Article> export(Article article) {
        return articleService.list(Wrappers.query(article));
    }
}
